package com.leehom.arch.storage.handler;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.aliyun.oss.OSS;
import com.aliyun.oss.model.OSSObject;
import com.aliyun.oss.model.OSSObjectSummary;
import com.aliyun.oss.model.ObjectListing;
import com.leehom.arch.storage.exception.FileStorageException;
import com.leehom.arch.storage.model.FileItem;
import com.leehom.arch.storage.model.Storage;

import lombok.Setter;
/**
 * 
 * @类名: AliOssFileStorageHandler
 * @说明: ali oss存储实现
 *
 * @author   leehom
 * @Date	 2018年3月13日 下午2:38:42
 * 修改记录：
 *
 * @see
 */

@Setter
public class AliOssFileStorageHandler extends BaseFileStorageHandler implements FileStorageHandler {
	
	private static Logger logger = LoggerFactory.getLogger(AliOssFileStorageHandler.class);
	
	private String bucketName;
	private Date urlExpirationData;
	private OSS ossClient;
	
	@Override
	public String schema() {
		return "oss";
	}

	@Override
	public InputStream getFile(String path, String fileName, Storage storage) {
		String objectName = this.contactPath(path, fileName);
		OSSObject ossObject = ossClient.getObject(bucketName, objectName);
		return ossObject.getObjectContent();
		
	}

	@Override
	public List<FileItem> listFileOfDir(String path, Storage storage) {
		//
		ObjectListing files = ossClient.listObjects(bucketName, path);
		//
		List<OSSObjectSummary> sums = files.getObjectSummaries();
		return this.toFileItems(sums, path);

	}
	
	@Override
	public void saveFile(String path, String fileName, InputStream inStream, Storage storage) throws FileStorageException {
		String objectName = this.contactPath(path, fileName);
		//
		ossClient.putObject(bucketName, objectName, inStream);
	}
	
	protected FileItem toFileItem(OSSObjectSummary file, String path) {
		//
		String fullPath = file.getKey();
		PathAndName pn = this.untactPath(fullPath);
		//
		FileItem item = new FileItem();
		item.setName(pn.getName());
		// 不直接用path，支持多级目录
		item.setPath(pn.getPath());
		item.setSize(file.getSize());
		return item;
	}
	
	protected List<FileItem> toFileItems(List<OSSObjectSummary> files, String path) {
		List<FileItem> items = new ArrayList<>();
		for(OSSObjectSummary file : files) {
			items.add(toFileItem(file, path));
		}
		return items;
	}

	@Override
	public String getUrl(String path, String fileName, Storage storage) {
		String objectName = this.contactPath(path, fileName);
		URL url = ossClient.generatePresignedUrl(bucketName, objectName, urlExpirationData);
		return url.toString();
	}

	@Override
	public void deleteFile(String path, String fileName, Storage storage) {
		String objectName = this.contactPath(path, fileName);
		ossClient.deleteObject(bucketName, objectName);
	}

	@Override
	public void deleteDir(String path, Storage storage) {
		List<FileItem> items = this.listFileOfDir(path, storage);
		for(FileItem item : items) {
			String objectName = this.contactPath(path, item.getName());
			ossClient.deleteObject(bucketName, objectName);
		}
	}

}
